package com.javacv.plus.extensions.image.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.image.ImageProcessor;
import com.javacv.plus.extensions.image.ProcessorResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;
import org.bytedeco.opencv.global.opencv_imgproc;

/**
 * 调整图像大小步骤
 */
@Slf4j
public class ResizeStep implements ProcessorStep<ImageProcessor, ProcessorResult> {

    private final int width;
    private final int height;

    public ResizeStep(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public ProcessorResult execute(ImageProcessor processor, ProcessorContext context) throws Exception {
        Mat currentImage = context.getAttribute("currentImage");
        if (currentImage == null) {
            throw new IllegalStateException("当前图像为空");
        }

        log.debug("调整图像大小: {}x{} -> {}x{}", 
                currentImage.cols(), currentImage.rows(), width, height);

        Mat resizedImage = new Mat();
        Size newSize = new Size(width, height);
        
        // 使用双线性插值进行缩放
        opencv_imgproc.resize(currentImage, resizedImage, newSize, 0, 0, opencv_imgproc.INTER_LINEAR);

        // 更新上下文中的当前图像
        context.setAttribute("currentImage", resizedImage);
        context.setAttribute("processedImage", resizedImage);

        log.debug("图像大小调整完成");
        return null;
    }

    @Override
    public String getStepName() {
        return "Resize";
    }

    @Override
    public int getPriority() {
        return 50;
    }
} 